jQuery(function ($) {
    
    function twbsPagination(totalPage) {
        //分页按钮
        Page({
			num:Math.ceil(totalPage / 30),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_b'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    $('.works_show').empty();
                    requestGoodsList(page - 1);
                    $('html,body').animate({scrollTop:$('.works_show ').offset().top-80}, 500);
                }
			}
		});
    };
   var url =  window.location.search;
    requestGoodsList(0);
    var pageOnOff = true;
    function requestGoodsList(page) {
        if(url){
            var catId = url.split('=')[1];
            var param={
                catId:catId,
                page:page,
                size:10
            };
        }else{
            var param={
                catId:'',
                page:page,
                size:10
            }; 
        }
       
        send_ajax('get', '/api/order/getRequest', param, function callback(data) {
            var html="";
            if(data.code==200){
                 //分页按钮
                  if (pageOnOff) {
                    twbsPagination(data.data.count,'')
                    pageOnOff = false;
                };
                var data  =data.data.requestList;
                console.log(data)
                for(var i = 0;i<data.length;i++){
                    var a_good = data[i];
                    if(a_good.street){
                        a_good.addr = a_good.street
                    }else if(a_good.distrit){
                        a_good.addr = a_good.distrit
                    }else if(a_good.city){
                        a_good.addr = a_good.city
                    }else if(a_good.province){
                        a_good.addr = a_good.province
                    }
                    
                    html+='  <li class="pur_list">\
                    <a target="_blank" href="/goods/pur_details.html?good_id='+a_good.id+'">\
                            <div class="pur_goods">\
                                <div class="pur_goods_name">'+a_good.title+'</div>\
                                <div class="pur_goods_num"><span>数量：</span><span>'+(a_good.count+a_good.unit_name)+'</span> </div>\
                            </div>\
                            <div class="pur_addr">\
                                <div class="Publisher"><span>发布人：</span> <span>'+a_good.nickname+'</span></div>\
                                <div class="pur_time"><span> 发布时间：</span><span>'+a_good.create_time+'</span></div>\
                            </div>\
                            <div class="pur_place"><span> 期待发货地：</span><span>'+a_good.addr+'</span></div>\
                            </a>\
                    </l.oi>'
                }
                $('.works_show').html(html);
            }
        });  
    };
    send_ajax('get', '/api/order/getRequestCatlist', '', function callback(res) { 
        if(res.code==200&&res.data){
            var catId = url.split('=')[1];
            var data = res.data;
            var html='';
            for(var i = 0;i<data.length;i++){
                var a_goods = data[i];
                html+=' <li><a target="_blank" href="/goods/pur_hall.html?catId='+a_goods.id+'"><span>'+a_goods.name+'</span><i></i></a></li>'
            }
             if(url){
                var catId = url.split('=')[1];
                for(var n = 0;n<data.length;n++){
                    var a_data = data[n];
                    if(a_data.id ==catId ){
                        $('.atp_choosed .left').append('<div class="pur_sort"><span>'+a_data.name+'</span><i class="closeing">X</i></div>')
                    }
                }
             }
            $('.pur_option').html(html);
        }
    });
    $(document).on('click', '.closeing', function () {
        $('.atp_choosed .left').html('');
        window.location.href=window.location.href.split(window.location.search)[0]; 
    })
    var bannerParam={
        positionSn:'index_banner_scroll',
        pageSn:'request_index',
        count:4
    }
    send_ajax('get', '/api/ad/list', bannerParam, function callback(res) { 
        if(res.data.length){
            var data = res.data;
            var html='';
            var bannerindex = '';
            for(var i = 0;i<data.length;i++){
                var a_data = data[i];
                bannerindex+='<span>'+(i+1)+'</span>'
                html+=' <a href="'+a_data.href+'" target="_blank" class="'+(i==0?'open':'')+'"><img src="'+a_data.img+'" alt="'+a_data.img_alt+'"></a>'
            }
            $('.banner_box').html(html);
            $('.small_box').html(bannerindex);
                    
            var index = 0 ;
            $('.small_box span').eq(0).addClass('btnColor');
            $('.small_box').on('click', 'span', function () {
                index = $(this).index();
                $('.small_box span').eq(index).addClass('btnColor').siblings().removeClass('btnColor');
                $('.banner_box a').eq(index).addClass('open').siblings().removeClass('open');
            });
            $('.small_box').on('click', 'img', function () {
                index = $(this).index();
                $('.banner_box a').eq(index).addClass('open').siblings().removeClass('open');
            });
            //banner图点击切换上一个下一个
            //  var index = 0;
            var bannerImgLength = $('.banner_box a').length;
            $('.next').click(function () {
                if (index < bannerImgLength-1 && index > -1) {
                    index++;
                    $('.small_box span').eq(index).addClass('btnColor').siblings().removeClass('btnColor');
                    $('.banner_box a').eq(index).addClass('open').siblings().removeClass('open');
                } else if (index == bannerImgLength-1) {
                    index = -1;
                    index++;
                    $('.small_box span').eq(index).addClass('btnColor').siblings().removeClass('btnColor');
                    $('.banner_box a').eq(index).addClass('open').siblings().removeClass('open');
                }
            });
            $('.pre').click(function () {
                if (index < bannerImgLength && index > 0) {
                    index--;
                    $('.small_box span').eq(index).addClass('btnColor').siblings().removeClass('btnColor');
                    $('.banner_box a').eq(index).addClass('open').siblings().removeClass('open');
                } else if (index == 0) {
                    index = bannerImgLength;
                    index--;
                    $('.small_box span').eq(index).addClass('btnColor').siblings().removeClass('btnColor');
                    $('.banner_box a').eq(index).addClass('open').siblings().removeClass('open');
                }
            });
        }
    });
});